package cn.bopomo.dealer.base.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuGroup;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;

import com.metaparadigm.jsonrpc.JSONSerializer;
import com.metaparadigm.jsonrpc.MarshallException;

public class AuthorizeModifyAction extends BaseAction {

	private String codeDescXMLPath = "/WEB-INF/code_description.xml";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		
		if (!this.hasPermission(request)){
			result.append(ResultCode.OP_NO_PRIMITION).append("||").append("你没有该操作权限，请与管理员联系");
			response.getWriter().write(result.toString());
			return null;
		}
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		String authorize_id = request.getParameter("aid");
		AuthorizeList authorize = os.getAuthorize(new Integer(authorize_id));
		String check_result = ResultCode.OP_FAILED;
		if (authorize != null){
			authorize.setModifyDate(new Date());
			String authorize_name = request.getParameter("authorize_name");
			String authorize_tag = request.getParameter("authorize_tag");
			String authorize_action = request.getParameter("authorize_action");
			authorize.setAuthorizeAction(authorize_action == null ? "" : authorize_action);
			authorize.setAuthorizeName(authorize_name == null ? "" : authorize_action);
			authorize.setAuthorizeTag(authorize_tag == null ? "" : authorize_tag);
			
			check_result = os.modifyAuthorize(authorize);
			
		}
		JSONObject json = new JSONObject();
	
		result.append(check_result).append("||").append(this.getCodeDescription(check_result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		response.getWriter().write(result.toString());
		return null;
	
	}

	public static void main(String[] args) {
		JSONObject json = new JSONObject();
		json.put("test1","hihi");
		json.put("aaa",1);
		JSONArray  array = new JSONArray();
		List list = new ArrayList();
		ModuleList module = new ModuleList();
		AuGroup group1 = new AuGroup();
		AuGroup group2 = new AuGroup();
		group1.setGroupName("group1");
		group2.setGroupName("group2");
		Set set = new HashSet();
		set.add(group1);
		set.add(group2);
		module.setAuGroups(set);
		AuthorizeList authorize1 = new AuthorizeList();
		AuthorizeList authorize2 = new AuthorizeList();
		authorize1.setAddDate(new Date());
		authorize1.setAuthorizeName("test2");
		authorize1.setAuthorizeTag("002");
		authorize2.setAddDate(new Date());
		authorize2.setAuthorizeName("test3");
		authorize2.setAuthorizeTag("003");
		list.add(authorize1);
		list.add(authorize2);
		//array.put(list);
		JSONSerializer jsonSerializer = new JSONSerializer();
		try {
			jsonSerializer.registerDefaultSerializers();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			array.put(jsonSerializer.toJSON(module));
		} catch (MarshallException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//array.put(authorize1);
		//array.put(json);
		System.out.println(json.toString());
		System.out.println(array);
	}
	
	
}
